<template>
  <div class="lb-goods-list">
    <top-nav />
    <div class="page-main">
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="姓名:" prop="user_name">
            <el-input
              v-model="searchForm.user_name"
              placeholder="请输入姓名"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
            >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
            >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >

                <el-table-column
                  prop="nickName"
                  label="微信昵称"
                  :min-width="120"
                ></el-table-column>
                <el-table-column prop="avatarUrl" label="微信头像" min-width="120">
                  <template slot-scope="scope">
                    <lb-image :src="scope.row.avatarUrl" />
                  </template>
                </el-table-column>
                <el-table-column
                  prop="type_msg"
                  label="类型"
                  :min-width="80"
                ></el-table-column>
                <el-table-column
                  prop="p_user"
                  label="上级代理"
                  :min-width="120"
                ></el-table-column>
                <el-table-column
                  prop="p_child_num"
                  label="下级代理"
                  :min-width="80"
                ></el-table-column>

                <el-table-column
                  prop="user_name"
                  label="姓名"
                  :min-width="120"
                ></el-table-column>
                <el-table-column
                  prop="phone"
                  label="手机号"
                  :min-width="120"
                ></el-table-column>
<!--                <el-table-column-->
<!--                  prop="agent_cash"-->
<!--                  label="代理分销佣金"-->
<!--                ></el-table-column>-->
                <el-table-column
                  prop="money"
                  label="消费金额"
                ></el-table-column>
                <el-table-column
                  prop="scale_msg"
                  label="抽成比例"
                ></el-table-column>
<!--                <el-table-column-->
<!--                  prop="status_msg"-->
<!--                  label="状态"-->
<!--                ></el-table-column>-->
                <el-table-column
                  prop="remark"
                  label="备注"
                ></el-table-column>
                <el-table-column prop="create_time" label="绑定时间" :min-width="120">
                  <template slot-scope="scope">
                    <p>{{ scope.row.create_time | handleTime(1) }}</p>
                    <p>{{ scope.row.create_time | handleTime(2) }}</p>
                  </template>
                </el-table-column>
<!--        <el-table-column prop="user_name" label="姓名" fixed></el-table-column>-->
<!--        <el-table-column prop="phone" label="电话" fixed></el-table-column>-->
<!--        <el-table-column prop="avatarUrl" label="微信头像" min-width="120">-->
<!--          <template slot-scope="scope">-->
<!--            <lb-image :src="scope.row.avatarUrl" />-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column-->
<!--          prop="nickName"-->
<!--          label="微信昵称"-->
<!--          :min-width="120"-->
<!--        ></el-table-column>-->

<!--        <el-table-column prop="city_type" label="申请类型" :min-width="120">-->
<!--          <template slot-scope="scope">-->
<!--            <el-tag :type="cityType[scope.row.city_type].type"-->
<!--            >{{ cityType[scope.row.city_type].text }}代理</el-tag-->
<!--            >-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column-->
<!--          prop="city"-->
<!--          label="申请加入的区域"-->
<!--          :min-width="120"-->
<!--        ></el-table-column>-->
<!--        <el-table-column prop="create_time" label="提交时间" :min-width="120">-->
<!--          <template slot-scope="scope">-->
<!--            <p>{{ scope.row.create_time | handleTime(1) }}</p>-->
<!--            <p>{{ scope.row.create_time | handleTime(2) }}</p>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column prop="status" label="阅读状态">-->
<!--          <template slot-scope="scope">-->
<!--            {{ statusType[scope.row.status] }}-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="操作" min-width="120">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                type="primary"
                plain
                @click="toShowDialog('sub', scope.row)"
                v-hasPermi="`${$route.name}-edit`"
              >{{ $t('action.edit') }}</lb-button>

              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmAgent('sub',scope.row.user_id)"
                v-hasPermi="`${$route.name}-delete`"
              >{{ $t('action.agentChild') }}</lb-button>

            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>

    <el-dialog
      title="编辑代理分销"
      :visible.sync="showDialog.sub"
      width="800px"
      center
      :append-to-body="true">
      <el-form
        class="dialog-form"
        :model="subForm"
        :rules="subFormRules"
        ref="subForm"
        label-width="140px"
      >
        <el-form-item label="真实姓名" prop="user_name">
          <el-input
            v-model="subForm.user_name"
            placeholder="请输入真实姓名"
            maxlength="5"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input
            v-model="subForm.phone"
            placeholder="请输入电话"
            maxlength="12"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="抽成比例" prop="scale">
          <el-input-number
            class="lb-input-number"
            :min="0"
            :max="300"
            :precision="0"
            :controls="false"
            v-model="subForm.scale"
            placeholder="请输入抽成比例"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group
            v-model="subForm.type"
          >
            <el-radio :label="1">代理</el-radio>
            <el-radio :label="2">商家</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="3"
            maxlength="300"
            resize="none"
            show-word-limit
            placeholder="请输入备注"
            v-model="subForm.remark"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="showDialog.sub = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('sub', 2)" v-preventReClick
        >确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="下级代理"
      width="900px" center
      :visible.sync="ShowChild.sub">
      <el-table :data="gridData">
        <el-table-column property="nickName"  label="微信昵称" width="80"></el-table-column>
        <el-table-column prop="avatarUrl" label="微信头像" min-width="80">
          <template slot-scope="scope">
            <lb-image :src="scope.row.avatarUrl" />
          </template>
        </el-table-column>
        <el-table-column property="type_msg" label="类型" width="80"></el-table-column>
        <el-table-column property="user_name" label="姓名" width="80"></el-table-column>
        <el-table-column property="phone" label="手机号" width="80"></el-table-column>
        <el-table-column
          prop="money"
          label="消费金额"
        ></el-table-column>
        <el-table-column
          prop="scale_msg"
          label="抽成比例"
        ></el-table-column>
        <el-table-column
          prop="remark"
          label="备注"
        ></el-table-column>
        <el-table-column prop="create_time" label="绑定时间" :min-width="120">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    return {
      loading: false,
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > (moment(moment(Date.now()).format('YYYY-MM-DD')).unix() + 24 * 3600 - 1) * 1000
        }
      },
      statusOptions: [
        {
          label: '全部',
          value: -1
        },
        {
          label: '未读',
          value: 0
        },
        {
          label: '已读',
          value: 1
        }
      ],
      cityType: {
        3: {
          type: 'success',
          text: '省'
        },
        1: {
          type: 'primary',
          text: '城市'
        },
        2: {
          type: 'danger',
          text: '区县'
        }
      },
      statusType: {
        0: '未读',
        1: '已读'
      },
      searchForm: {
        page: 1,
        limit: 10,
        start_time: '',
        end_time: '',
        name: '',
        status: -1
      },
      tableData: [],
      total: 0,
      showDialog: { sub: false, user: false, pid: false },
      subForm: {
        id: 0,
        user_name: '',
        phone: '',
        scale: '',
        type: '',
        remark: ''
      },
      subFormRules: {
        user_name: { required: true, validator: this.$reg.isNotNull, reg_type: 2, text: '真实姓名', trigger: 'blur' },
        phone: { required: true, validator: this.$reg.isTel, text: '请输入电话', trigger: 'blur' },
        scale: { required: true, type: 'number', message: '请输入抽成比例', trigger: 'blur' },
        type: { required: true, type: 'number', message: '请输选择类型', trigger: 'blur' },
      },
      ShowChild: { sub: false, user: false, pid: false },
      gridData:[],
      agent:{
        user_id:0
      }
    }
  },
  created () {
    this.getTableDataList(1)
  },
  methods: {
    //搜索或提交
    async submitForm (key, validate = 1) {
      let flag = true
      if (validate === 1) {
        this.$refs[`${key}Form`].validate(valid => {
          if (!valid) flag = false
        })
      }
      if (flag) {
        let subForm = JSON.parse(JSON.stringify(this[`${key}Form`]))

        this.$api.agent['agentDistributeEdit'](subForm).then(res => {
          if (res.code === 200) {
            this.showDialog[key] = false
            this.$message.success("设置成功");
            this.getTableDataList(1)
          }else {
            this.$message.error(res.msg);
          }
        })

        // let { code} = await this.$api.agent['agentDistributeEdit'](subForm)
        // if (code !== 200) return
        // this.$message.success(msg)
        // this.showDialog[key] = false
      }
    },
    resetForm (form) {
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async toChange (index) {
      this.searchForm.status = index
      this.getTableDataList(1)
    },
    //编辑
    async toShowDialog (key, item = {}) {
      item = JSON.parse(JSON.stringify(item))
      if (key === 'sub') {
        for (let keys in this[`${key}Form`]) {
          this[`${key}Form`][keys] = item[keys]
        }
      }
      this.showDialog[key] = true
    },
    //下级代理
    async confirmAgent (key, item = {}) {
      item = JSON.parse(JSON.stringify(item))
      this.agent.user_id = item
      console.log(item)
      if (key === 'sub') {
        let { code, data } = await this.$api.distribution.getAgent(this.agent)
        console.log(code)
        console.log(data)
        if (code === 200) {
          this.gridData = data.list
        }
      }
      this.ShowChild[key] = true
    },

    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.tableData = []
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let { start_time: time } = searchForm
      if (time && time.length > 0) {
        searchForm.start_time = time[0] / 1000
        searchForm.end_time = time[1] / 1000
      } else {
        searchForm.start_time = ''
        searchForm.end_time = ''
      }
      if (searchForm.status === -1) {
        delete searchForm.status
      }
      let { code, data } = await this.$api.agent.agentDistributeList(searchForm)
      this.loading = false
      if (code !== 200) return
      console.log('data.list', data.list);
      this.tableData = data.list
      this.total = data.total
    },
    async updateItem (id, status) {
      this.$api.agent.agentApplyCheck({ id, status }).then((res) => {
        if (res.code === 200) {
          this.$message.success(this.$t('tips.successOper'))
        }
        this.getTableDataList()
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  .el-input {
    width: 200px;
  }
  .el-image {
    width: 120px;
    height: 120px;
  }
  .el-textarea {
    width: 600px;
  }
}
</style>
